<?php

namespace app\api\controller\v2;

use app\admin\model\cms\GrapevineModel;
use app\admin\model\cms\IntroduceModel;
use app\admin\model\cms\Live;
use app\admin\model\cms\Platepost;
use app\admin\model\Investment;
use app\common\controller\Api;
use app\common\model\Channel;

/**
 * 侧边栏接口
 */
class Sidebar extends Api
{
    //无需登录的方法,同时也就不需要鉴权了
    protected $noNeedLogin = ['getWebConfig'];
    //无需鉴权的方法,但需要登录
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!\think\Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    //获取侧边栏信息
    public function getSidebar()
    {
        $data = Channel::where('is_sidebar', 1)
            ->where('is_del', 0)
            ->where('parent_id', '>', 0)
            ->with([
                'parent'=>function($query){
                    $query->field('id,parent_id,name,as_name');
                }
            ])
            ->field('id,name,as_name,parent_id,correlation_id')
            ->order('list_order', 'asc')//排序|数字越小排序越靠前
            ->select();

        //
        $res = [];
        foreach ($data as $v) {
            $model = [];
            switch ($v['parent_id']) {
                case '2'://量化辅助
                    $model = $this->getIntroduce($v['id']);
                    break;
                case '3'://债市广场
                    $model = $this->getPlatePost($v['correlation_id']);
                    break;
                case '4'://成长计划
                    $model = $this->getVideo($v['correlation_id']);
                    break;
                case '6'://直播专区
                    $model = $this->getLive($v['correlation_id']);
                    break;
                case '7'://数据库
                    $model = $this->getInvestment($v['correlation_id']);
                    break;
                case '8'://小道消息的channels.id固定=9 parent_id=8
                    $model = $this->getGrapevine();
                    break;
            }
            $_arr = $v->toArray();
            $_arr['list'] = $model;
            $res[] = $_arr;
        }


        $this->success('', $res);
    }

    /**
     * 获取债市论坛推荐
     */
    private function getPlatePost(int $category_id)
    {
        $data = Platepost::where('plateid', $category_id)
            ->where('is_audit', 1)
            ->where('is_audit', 1)
            ->where('is_sidebar', 1)
            ->order('id', 'desc')
            ->limit(6)
            ->select();

        $res = [];
        foreach ($data as $v) {
            $file_img = '/img/default_user.jpg';
            if (!empty($v['file_img'])) {
                if (!empty(explode(',', $v['file_img'])[0])) {
                    $file_img = explode(',', $v['file_img'])[0];
                }
            }
            $file_img = obsurl($file_img);
            $res[] = [
                'id' => $v['id'],
                'image' => $file_img,
                'title' => $v['postname'],
                'type' =>  'article',
            ];
        }

        return $res;
    }

    //成长计划-视频
    private function getVideo(int $category_id)
    {
        $data = \app\admin\model\cms\Video::where('video_category_id', $category_id)
            ->where('is_sidebar', 1)
            ->order('id', 'desc')
            ->limit(6)
            ->select();

        $res = [];
        foreach ($data as $v) {
            $file_img = '/img/default_user.jpg';
            if (!empty($v['image'])) {
                $file_img = $v['image'];
            }
            $file_img = obsurl($v['image']);
            $res[] = [
                'id' => $v['id'],
                'image' => $file_img,
                'title' => $v['title'],
                'type' =>  'video',
            ];
        }

        return $res;
    }

    //直播
    private function getLive(int $category_id)
    {
        $data = Live::where('live_category_id', $category_id)
            ->where('is_sidebar', 1)
            ->order('id', 'desc')
            ->limit(6)
            ->select();

        $res = [];
        foreach ($data as $v) {
            $file_img = '/img/default_user.jpg';
            if (!empty($v['image'])) {
                if (!empty(explode(',', $v['image'])[0])) {
                    $file_img = explode(',', $v['image'])[0];
                }
            }
            $file_img = obsurl($file_img);
            $res[] = [
                'id' => $v['id'],
                'image' => $file_img,
                'title' => $v['title'],
                'type' =>  'live',
            ];
        }

        return $res;
    }

    //投研日志(数据库)
    private function getInvestment(int $category_id)
    {
        $data = Investment::where('investment_category_id', $category_id)
            ->where('is_sidebar', 1)
            ->order('id', 'desc')
            ->limit(6)
            ->select();

        $res = [];
        foreach ($data as $v) {
            $file_img = '/img/default_user.jpg';
            if (!empty($v['file_img'])) {
                if (!empty(explode(',', $v['file_img'])[0])) {
                    $file_img = explode(',', $v['file_img'])[0];
                }
            }
            $file_img = obsurl($file_img);
            $title = '无标题';

            if (!empty($v['macro_content'])) {
                $title = strip_tags($v['macro_content']);
            } elseif (!empty($v['policy_content'])) {
                $title = strip_tags($v['policy_content']);
            } elseif (!empty($v['fund_content'])) {
                $title = strip_tags($v['fund_content']);
            } elseif (!empty($v['emotion_content'])) {
                $title = strip_tags($v['emotion_content']);
            } elseif (!empty($v['expect_content'])) {
                $title = strip_tags($v['expect_content']);
            } elseif (!empty($v['news_content'])) {
                $title = strip_tags($v['news_content']);
            }
            $title = mb_strimwidth($title, 0, 10);

            $res[] = [
                'id' => $v['id'],
                'image' => $file_img,
                'title' => $title,
                'type' =>  'article',
            ];
        }

        return $res;
    }

    //分类简介表-直播、量化辅助、数据库、成长计划的介绍
    public function getIntroduce(int $channels_id){
        $data = IntroduceModel::where('channels_id', $channels_id)
            ->where('is_sidebar', 1)
            ->order('id', 'desc')
            ->limit(6)
            ->select();

        $res = [];
        foreach ($data as $v) {
            $file_img = '/img/default_user.jpg';
            if (!empty($v['file_img'])) {
                if (!empty(explode(',', $v['file_img'])[0])) {
                    $file_img = explode(',', $v['file_img'])[0];
                }
            }
            $file_img = obsurl($file_img);
            $title = '无标题';
            if (!empty($v['content'])) {
                $title = strip_tags($v['content']);
            }
            $title = mb_strimwidth($title, 0, 10);

            $res[] = [
                'id' => $v['id'],
                'image' => $file_img,
                'title' => $title,
                'type' =>  'article',
            ];
        }

        return $res;
    }

    //小道消息
    public function getGrapevine(){
        $data = GrapevineModel::where('status',1)
            ->where('is_show',1)
            ->order('weigh','desc')
            ->order('id','desc')
            ->limit(6)
            ->select();

        $res = [];
        foreach ($data as $v) {
            $file_img = '/img/default_user.jpg';
            if (!empty($v['image'])) {
                if (!empty(explode(',', $v['image'])[0])) {
                    $file_img = explode(',', $v['image'])[0];
                }
            }
            $file_img = obsurl($file_img);

            $res[] = [
                'id' => $v['id'],
                'image' => $file_img,
                'title' =>  date('Y-m-d H:i',$v['createtime']) .' '. $v['title'],
                'type' =>  'gossip',
                'form_url' => $v['form_url']
            ];
        }

        return $res;
    }


}
